/**
 * User: brianjeppesen
 * Date: 6/26/11
 * Time: 11:26 AM
 */

authors = new Array();
function add_author(name, bio) { authors.push(new Array(name, bio)); }

larbimage = new Array();
function add_image(link, description, dLink, artist, year) { larbimage = [link, description, dLink, artist, year]; }

books = new Array();
function add_book(isbn, author, title, publisher, date, pages, img) { books.push(new Array(isbn, author, title, publisher, date, pages, img)); }

function show_article_header()
{
    show_authors();
    show_photo_w_caption();
    show_book_details();
}
function show_article_footer()
{
    show_author_bios();
    show_image_credit();
    show_letters_note();
    show_purchase_boxes();
}

function show_authors()
{
    document.write("<center><b>");
    var author;
    for(author=0; author < authors.length; author++)
    {
        if(author>0)
        {
            if((authors.length-author)<2) { document.write(" and "); }
            else { document.write(", "); }
        }
        document.write(authors[author][0]);
    }
    document.write("</b></center><br/>");
}

function show_photo_w_caption()
{
    document.write("<img src=\"" + larbimage[0] + "\" />" +
            "<center><div class=\"photoCaption\">  <a href=\"" + larbimage[2] + "\"><i>" + larbimage[1] + " </i> &copy; " + larbimage[3] + " " + larbimage[4] + " </a></div></center><br/>");
}

function show_book_details()
{
    var book;
    for(book = 0; book < books.length; book++)
    {
        show_book_info(books[book][1], books[book][2], books[book][3], books[book][4], books[book][5]);
    }
}
function show_book_info(author, title, publisher, date, pages)
{
    document.write("<b>" + author + "<br/><i>" + title + "</i></b><br/>" +
            publisher + ", " + date + ". " + pages + " pp.<br/><br/>");
}

function show_author_bios()
{
    var author;
    for(author=0; author < authors.length; author++)
    {
        document.write("<b>" + authors[author][0] + "</b> " + authors[author][1] + "<br/><br/>");
    }
}

function show_image_credit()
{
    document.write("<b>Image: </b><a href=\"" + larbimage[2] + "\" target=\"_blank\">" + larbimage[1] + " &copy; " + larbimage[3] + " " + larbimage[4] + "&#160;" + larbimage[2] + "</a><br/><br/>");
}

function show_letters_note()
{
    document.write("<center><b>We welcome letters to the editors, on this or any other subject, at <a href=\"mailto:letters@lareviewofbooks.org\" target=\"_blank\"><b>letters@lareviewofbooks.org.</b></a></b></center><br/><br/>");
}

function show_purchase_boxes()
{
    if(books.length > 0)
    {
        document.write("Buying books by clicking on the icons below helps support the <i>Los Angeles Review of Books.</i>  " +
            "Any item you buy after clicking through pays us a royalty, which we in turn use to pay our bills.  " +
            "If you would like to make a more substantial gift, please hit the " +
            "<a href=\"http://lareviewofbooks.org/donate\" target=\"_blank\">DONATE button here</a>, above, or below.<br/><br/>");
        var book;
        for(book = 0; book < books.length; book++)
        {
            purchase_book_box(books[book][2], books[book][1], books[book][0], books[book][6]);
        }
    }
}

function purchase_book_box(title, author, isbn, img)
{
    if(img != null)
    {
        img_src = "<img src=\"" + img + "\" />";
    }
    else
    {
        img_src = get_amazon_image(isbn);
    }
    
    document.write(
            "<div class=\"purchase_info\">" +
                    get_purchase_info(title, author) +
                    "</div>" +
                    "<div class=\"purchase_links\">" +
                    "<ul>" +
                    "<li>" + get_indiebound_link(isbn) + "</li>" +
                    "<li>" + get_powells_link(isbn) + "</li>" +
                    "<li>" + get_amazon_link(isbn) + "</li>" +
                    "<li>" + get_bn_link(isbn) + "</li>" +
                    "</ul>" +
                    "</div>" +
                    "<div class=\"purchase_img\">" + img_src + "</div>" +
                    "<div class=\"clearance\"></div>"
    );
}

function get_purchase_info(title, author)
{
    return ("<p class=\"purchase_book_title\">" + title + "</p>" + "<p class=\"purchase_book_author\">" + author + "</p>");
}

var indiebound_id;
function set_indiebound_id(id)
{
    indiebound_id = id;
}

function get_indiebound_link(isbn)
{
    return ("<a href=\"http://www.indiebound.org/book/" + isbn + "?aff=" + indiebound_id + "\" target=\"_blank\">Buy on IndieBound</a>");
}

var powells_id;
function set_powells_id(id)
{
    powells_id = id;
}

function get_powells_link(isbn)
{
    return ("<a href=\"http://www.powells.com/partner/" + powells_id + "/biblio/" + isbn + "?p_cv\"  rel=\"powells-" +
            isbn + "\" target=\"_blank\">Buy on Powells</a>");
}

var bn_id;
function set_bn_id(id)
{
    bn_id = id;
}

function get_bn_link(isbn)
{
    return ("<a href=\"http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ean=" + isbn + "&cm_mmc=AFFILIATES-_-Linkshare-_-" + bn_id + "-_-10:1\"" +
            " target=\"_blank\">Buy on Barnes & Noble</a>");
}

var amazon_id;
function set_amazon_id(id)
{
    amazon_id = id;
}

function get_amazon_link(isbn)
{
    return ("<a href=\"http://www.amazon.com/gp/product/" + isbn +
            "/ref=as_li_tf_il?ie=UTF8&amp;tag=" + amazon_id + "&amp;linkCode=as2&amp;camp=217145&amp;creative=399349&amp;creativeASIN=" +
            isbn + "\" target=\"_blank\">Buy on Amazon</a>");
}

function get_amazon_image(isbn)
{
    return ("<img src=\"http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&Format=_SL160_&ASIN=" + isbn + "&ID=AsinImage&WS=1&ServiceVersion=20070822\" />");
}


function addCss(cssCode) {
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if(styleElement.styleSheet) {
        styleElement.styleSheet.cssText = cssCode;
    }
    else {
        styleElement.appendChild(document.createTextNode(cssCode));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function purchase_box_setup(amazon_id, bn_id, indiebound_id, powells_id) {
    set_amazon_id(amazon_id);
    set_bn_id(bn_id);
    set_indiebound_id(indiebound_id);
    set_powells_id(powells_id);

    addCss(
            ".purchase_book_title, .purchase_book_author, .purchase_info h1, .purchase_info h2, .purchase_links, .purchase_links ul, .purchase_links ul li, .purchase_img, .purchase_img img {"+
                "margin:  0;"+
                "padding:  0;"+
                "font-family: 'Trebuchet MS', 'sans serif';"+
            "}"+

            ".purchase_info, .purchase_links ul li {"+
                "padding: 3px 7px 3px 7px;"+
                "background-color: #000000;"+
            "}"+

            ".purchase_book_title, .purchase_book_author, .purchase_info h1, .purchase_info h2, .purchase_links ul li, .purchase_links ul li a {"+
                "font-size: 11pt;"+
                "line-height: 12pt;"+
                "text-align: left;"+
                "color: #ffffff;"+
            "}"+

            ".purchase_info {"+
                "float: left;"+
                "width: 170px;"+
                "margin: 25px 0 0 76px;"+
                "border-right: none;"+
            "}"+

            ".purchase_book_title, .purchase_info h1 {"+
                "margin-bottom: 10px;"+
                "font-weight: normal;"+
                "text-transform: uppercase;"+
            "}"+
            ".purchase_book_author, .purchase_info h2 {"+
                "letter-spacing: 0;"+
                "font-weight: normal;"+
                "text-transform: capitalize;"+
                "text-align: right;"+
            "}"+

            ".purchase_links {"+
                "float: right;"+
                "width: 155px;"+
                "margin: 25px 103px 0 0;"+
            "}"+

            ".purchase_links ul {"+
                "list-style-position: outside;"+
                "list-style-image: none;"+
                "list-style-type: none;"+
                "text-indent: 0;"+
            "}"+

            ".purchase_links ul li {"+
                "width: 155px;"+
                "border-left: none;"+
            "}"+

            ".purchase_img {"+
                "width: 100px;"+
                "margin-left: 260px;"+
                "margin-right: 210px;"+
            "}"+

            ".purchase_img img {"+
                "width: 100px;"+
                "border: 1px solid #000000;"+
            "}"+

            ".clearance {"+
                "height: 20px;"+
                "clear:both;"+
            "}"
    );
}